# -*- coding: utf-8 -*-
"""
@Author : xh
@Time   : 2023/9/27 06:22
"""
import copy
from mathsolver.functions.base import *


class ShuLieNumSumsUpdate001(BaseFunction):
    """
    已知等差数列\\{a_{n}\\}的前3项和为6,前8项和为-4.求数列\\{a_{n}\\}的通项公式.
    """
    def solver(self, *args):
        assert len(args) == 3
        seq = args[0].sympify()
        num = args[1].sympify()
        value = args[2].sympify()
        seq_name = seq.func
        sl = self.search(str(seq_name))
        s_name = sl.sName
        if s_name is None:
            s_name = "S_"
            del self.known[str(seq_name)]
            sl.sName = s_name
            sl.name = str(seq_name) + s_name
            self.output.append(sl)
        s_name = sympify(s_name)
        answer = {}
        if 'Sequations' in self.known:
            seqs = self.search('Sequations')
        else:
            seqs = []
        seqs.append([s_name(num), value])
        answer['Sequations'] = seqs
        self.output.append(BaseShuLieValue(answer))
        return self


class ShuLieNumSumsUpdate002(BaseFunction):
    """
    已知等比数列的公比为2,且前5项和为1,则前10项的和等于(   )
    """
    def solver(self, *args):
        assert len(args) == 2
        isinstance(args[0], BaseNumber)
        num = args[0].sympify()
        value = args[1].sympify()
        sls = [self.known[item] for item in self.known if isinstance(self.known[item], BaseSequence)]
        assert len(sls) == 1
        sl = sls[0]
        s_name = sl.sName
        if s_name is None:
            s_name = "S_"
            del self.known[str(sl.name)]
            sl.sName = s_name
            sl.name = str(sl.aName) + s_name
            self.output.append(sl)
        s_name = sympify(s_name)
        answer = {}
        if 'Sequations' in self.known:
            seqs = self.search('Sequations')
        else:
            seqs = []
        seqs.append([s_name(num), value])
        answer['Sequations'] = seqs
        self.output.append(BaseShuLieValue(answer))
        return self


class ShuLieNumSumsUpdate(BaseFunction):
    CLS = [ShuLieNumSumsUpdate001, ShuLieNumSumsUpdate002]

    def solver(self, *args):
        r = None
        for cl in ShuLieNumSumsUpdate.CLS:
            try:
                r = cl(known=self.known, verbose=True).solver(*args)
                break
            except Exception:
                pass
        if not r:
            raise 'try fail'
        return r
